package com.dmz.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmz.workflow.base.BaseServiceImpl;
import com.dmz.workflow.dao.WorkflowProcessStatusMapper;
import com.dmz.workflow.service.WorkflowProcessStatusService;
import com.dmz.workflow.util.ObjectUtils;
import com.echronos.workflow.constant.ProcessConstants;
import com.dmz.workflow.entity.ProcessInstanceStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class WorkflowProcessStatusServiceImpl extends BaseServiceImpl<WorkflowProcessStatusMapper, ProcessInstanceStatus> implements WorkflowProcessStatusService {

    @Autowired
    private WorkflowProcessStatusMapper workflowProcessStatusMapper;

    private LambdaQueryWrapper<ProcessInstanceStatus> lambdaQueryWrapper;

    private LambdaUpdateWrapper<ProcessInstanceStatus> lambdaUpdateWrapper;

    @Override
    public Integer selectStatusByProcessInstanceId(String processInstanceId) {
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProcessInstanceStatus::getProcessInstanceId, processInstanceId);
        ProcessInstanceStatus processInstanceStatus = workflowProcessStatusMapper.selectOne(lambdaQueryWrapper);
        if (ObjectUtils.isNotEmpty(processInstanceStatus)) {
            return processInstanceStatus.getStatus();
        }
        return ProcessConstants.PROCESS_APPROVING_STATUS;
    }

    @Override
    public int updateStatusByProcessInstanceId(String processInstanceId, Integer status) {
        lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ProcessInstanceStatus::getProcessInstanceId, processInstanceId);
        ProcessInstanceStatus processInstanceStatus = new ProcessInstanceStatus();
        processInstanceStatus.setStatus(status);
        return workflowProcessStatusMapper.update(processInstanceStatus, lambdaUpdateWrapper);
    }

    @Override
    public IPage<ProcessInstanceStatus> selectProcessInstanceIdsByStatus(String memberId, Integer status, Integer pageNo, Integer pageSize, String keyword) {
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtils.isNotEmpty(status), ProcessInstanceStatus::getStatus, status)
                .eq(ProcessInstanceStatus::getStartUser, memberId);
        if (StringUtils.isNotEmpty(keyword)) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(ObjectUtils.isNotEmpty(keyword), ProcessInstanceStatus::getProcessName, keyword)
                    .or()
                    .like(ObjectUtils.isNotEmpty(keyword), ProcessInstanceStatus::getMemberName, keyword));
        }
        lambdaQueryWrapper.orderByDesc(ProcessInstanceStatus::getCreatedTime);
        IPage<ProcessInstanceStatus> instanceStatusIPage = new Page<>(pageNo + 1, pageSize);
        return workflowProcessStatusMapper.selectPage(instanceStatusIPage, lambdaQueryWrapper);
    }

    @Override
    public List<ProcessInstanceStatus> selectProcessesStatus(Set<String> processInstanceIds) {
        if (ObjectUtils.isEmpty(processInstanceIds)) {
            return null;
        }
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ProcessInstanceStatus::getProcessInstanceId, processInstanceIds);
        return workflowProcessStatusMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public ProcessInstanceStatus selectInstanceByInstanceId(String processInstanceId) {
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProcessInstanceStatus::getProcessInstanceId, processInstanceId);
        return workflowProcessStatusMapper.selectOne(lambdaQueryWrapper);
    }
}
